//
//  LrcDecode.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/11/10.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "LrcDecode.h"

@implementation LrcDecode


- (instancetype)initWithLrc:(NSString *)lrc{

    if (self = [super init]) {
     
        BOOL isDecodeSuccess = NO;
        
        if (lrc&&[lrc isKindOfClass:[NSString class]]&&[lrc length]>0) {
            
           NSRange starRange=[lrc rangeOfString:@"["];
           NSRange stopRange=[lrc rangeOfString:@"]"];
           if (starRange.location!=NSNotFound&&stopRange.location!=NSNotFound) {
                self.timeStr = [lrc substringWithRange:NSMakeRange(starRange.location + 1, stopRange.location - starRange.location - 1)];
               
               NSString *minString = [self.timeStr substringWithRange:NSMakeRange(0,2)];
               NSString *secString = [self.timeStr substringWithRange:NSMakeRange(3,2)];
               NSString *mseString = [self.timeStr substringWithRange:NSMakeRange(6,2)];
               
               self.timeLength = [minString floatValue] * 60 + [secString floatValue] + [mseString floatValue] / 1000;
               
               self.lrcStr = [lrc substringFromIndex:10];
               
               isDecodeSuccess = YES;
           }
            
        }
        
        self.success = isDecodeSuccess;
        
    }
    return self;
}

+ (NSArray <LrcDecode *> *)convertLrcStringToOjbect:(NSString *)lrcString{

    if (lrcString&&[lrcString isKindOfClass:[NSString class]]&&[lrcString length]>0) {
        
        __block NSMutableArray *lrcArray = [NSMutableArray arrayWithCapacity:0];
        
        NSArray *lycArray = [lrcString componentsSeparatedByString:@"\n"];
        [lycArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            LrcDecode *item = [[LrcDecode alloc] initWithLrc:obj];
            if (item.success) {
                [lrcArray addObject:item];
            }
        }];
        
        return lrcArray;
    }
    
    return nil;
}

@end
